package com.dyrj.question.controller;

import com.dyrj.utils.ExcelExportHelper;
import com.dyrj.utils.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.activation.FileTypeMap;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.dyrj.question.service.IPTkglService;
import com.dyrj.question.bean.PTkgl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 题库管理 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Controller
@RequestMapping("/question/p-tkgl")
public class PTkglController {

    @Resource
    private IPTkglService service;

    @GetMapping("/index")
//    @RequiresPermissions("question:p-tkgl:index")
    public String index(ModelMap map) {
    return "question/index_p-tkgl";
    }

    /**
    * @Title: table
    * @Description: (表格数据) 列表
    * @return PageInfo<PTkgl>    返回类型
    */
    @GetMapping("/table")
    @ResponseBody
//    @RequiresPermissions("question:p-tkgl:index")
    public PageResultMsg table(PTkgl record) {
        return service.table(record);
    }

    /**
    * @Title: add
    * @Description: (新增)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PostMapping("/")
    @ResponseBody
//    @RequiresPermissions("question:p-tkgl:add")
    public ResultMsg add(PTkgl record) {
        return service.add(record);
    }

    /**
    * @Title: info
    * @Description: (详情)
    * @param id
    * @return PTkgl    返回类型
    */
    @GetMapping("/{id}")
    @ResponseBody
//    @RequiresPermissions("question:p-tkgl:info")
    public PTkgl info(@PathVariable String id) {
        return service.info(id);
    }

    /**
    * @Title: update
    * @Description: (更新)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PutMapping("/")
    @ResponseBody
//    @RequiresPermissions("question:p-tkgl:edit")
    public ResultMsg update(PTkgl record) {
        return service.update(record);
    }

    /**
    * @Title: delete
    * @Description: (删除)
    * @param id
    * @return ResultMsg    返回类型
    */
    @DeleteMapping("/{id}")
    @ResponseBody
//    @RequiresPermissions("question:p-tkgl:delete")
    public ResultMsg delete(@PathVariable String id) {
        return service.delete(id);
    }

    @RequestMapping("/doExport")
    public void doExport(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pkid = request.getParameter("pkid");
        HashMap map = new HashMap();
        map.put("fkid",pkid);
        PageResultMsg pageResultMsg = service.table(null);
        List list = (List)pageResultMsg.getData();
        FileInputStream in = null; // 输入流
        ServletOutputStream out = null;
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        String outPutFilePath="";
        HSSFWorkbook outworkbook=excelExportHelper.exportExceltoMode("","");
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
        FileOutputStream fOut = new FileOutputStream("");
        outworkbook.write(fOut);
        fOut.flush();
        fOut.close();
        File file=new File("");
        if (file.exists()) {
            response.setHeader("Connection", "close");
            response.setContentType(FileTypeMap.getDefaultFileTypeMap()
                    .getContentType(StringUtil.getFileName(outPutFilePath))+ "; charset=UTF-8");
            response.setHeader("Content-disposition", "attachment; filename="
                    + new String(StringUtil.getFileName(outPutFilePath).getBytes("GBK"),
                    "ISO8859_1"));
            in = new FileInputStream(outPutFilePath); // 读入文件
            out = response.getOutputStream();
            out.flush();
            int aRead = 0;
            while ((aRead = in.read()) != -1 & in != null) {
                out.write(aRead);
            }
            out.flush();
            out.close();
            in.close();
        }
    }
}
